
getrukuInfo();
function getrukuInfo() {
    Ajax({
        url: "http://localhost/h5/php/RukuInfo.php",
        success(res) {
            sessionStorage.setItem('rukuInfo', res)
            res = JSON.parse(res)
            // 页面设置
            sessionStorage.setItem('pageCurrent', 1);
            // 计算总页数
            const pageCount = Math.ceil(res.length / 10);
            //存储总页数
            sessionStorage.setItem('pageCount', pageCount);
            //一页十条数据显示
            let result = res.slice(0, 10);
            //渲染数据
            console.log(result);
            callRukuInfo(result);
        }       
    })
}
function allRukuInfo(){
    Ajax({
        url: "http://localhost/h5/php/RukuInfo.php",
        success(res) {
            sessionStorage.setItem('rukuInfo', res)
            res = JSON.parse(res)
            // 页面设置
            sessionStorage.setItem('pageCurrent', 1);
            // 计算总页数
            const pageCount = Math.ceil(res.length / 10);
            //存储总页数
            sessionStorage.setItem('pageCount', pageCount);
            //一页十条数据显示
            let result = res.slice(0, 10);
            //渲染数据
            console.log(result);
            callRukuInfo(result);
        }       
    })
}

// 查询
function rukuQuery() {
    let rukunumber1 = document.querySelector('.rukunumber');
    let rukunumber = rukunumber1.value
    console.log(rukunumber);
    if (!rukunumber) return alert('请输入单号')
    Ajax({
        url: "http://localhost/h5/php/RukuQuery.php",
        data: { rukunumber },
        success(res) {
            sessionStorage.setItem('rukuInfo', res)
            let result = JSON.parse(res)
            sessionStorage.setItem('pageCurrent', 1);
            const pageCount = Math.ceil(result.length / 10);
            sessionStorage.setItem('pageCount', pageCount);
            result = result.slice(0, 10);
            console.log(result);
            callRukuInfo(result);
            rukunumber1.value = ''
        }
    })
}

// 首页
function firstPage() {
    //设置当前页 pageCurrent
    sessionStorage.setItem('pageCurrent', 1)
    let result = JSON.parse(sessionStorage.getItem('rukuInfo'));
    result = result.slice(0, 10)
    callRukuInfo(result);
}
//上一页
function upPage() {
    let pageCurrent = +sessionStorage.getItem('pageCurrent') - 1
    let rukuInfo = JSON.parse(sessionStorage.getItem('rukuInfo'))
    //判断是否是第一页
    if (pageCurrent < 1) {
        pageCurrent = 1
    }
    sessionStorage.setItem('pageCurrent', pageCurrent);
    //计算上一页的数据
    let result = rukuInfo.slice((pageCurrent - 1) * 10, pageCurrent * 10)
    callRukuInfo(result);
}
//下一页
function downPage() {
    let pageCurrent = +sessionStorage.getItem('pageCurrent') + 1;
    let rukuInfo = JSON.parse(sessionStorage.getItem('rukuInfo'))
    //获取最后一页
    let pageCount = +sessionStorage.getItem('pageCount')
    if (pageCurrent > pageCount) {
        pageCurrent = pageCount
    }
    sessionStorage.setItem('pageCurrent', pageCurrent);
    let result = rukuInfo.slice((pageCurrent - 1) * 10, pageCurrent * 10)
    callRukuInfo(result);
}
//尾页
function lastPage() {
    let pageCount = +sessionStorage.getItem('pageCount')
    sessionStorage.setItem('pageCurrent', pageCount);
    let rukuInfo = JSON.parse(sessionStorage.getItem('rukuInfo'))
    let result = rukuInfo.slice((pageCount - 1) * 10);
    callRukuInfo(result);
}

//添加功能
function addrukuInfo() {
    addrukuNumber = document.querySelector('#addrukuNumber').value;
    addrukuName = document.querySelector('#addrukuName').value;
    addrukuDate = document.querySelector('#addrukuDate').value;
    addrukuWhat = document.querySelector('#addrukuWhat').value;
    addrukuOverhouse = document.querySelector('#addrukuOverhouse').value;
    console.log(addrukuNumber, addrukuOverhouse);
    //非空判断
    if (!(addrukuOverhouse || addrukuDate || addrukuName || addrukuNumber)) return alert('请输入完整数据')
    Ajax({
        url: "http://localhost/h5/php/RukuAddInfo.php",
        type: "POST",
        data: { addrukuOverhouse, addrukuDate, addrukuName, addrukuNumber, addrukuWhat },
        success(res) {
            console.log(res);
            location.reload();
        }
    })
}
//修改和删除功能
addrukudelete();
function addrukudelete() {
    const tbody = document.querySelector('tbody');
    tbody.addEventListener('click', function (e) {
        e = e || window.event
        //删除
        if (e.target.dataset.o == 'del') {
            const flag = window.confirm('你确定要删除吗')
            if (flag) {
                Ajax({
                    url: "http://localhost/h5/php/RukudeleteInfo.php",
                    data: { id: e.target.dataset.id },
                    success(res) {
                        console.log(res);
                        location.reload();
                    }
                })
            }
        }
        //修改获取
        if (e.target.dataset.o == 'update') {
            Ajax({
                url: "http://localhost/h5/php/RukuGetAmend.php",
                data: { id: e.target.dataset.id },
                success(res) {
                    res = JSON.parse(res)
                    // console.log(res);
                    console.log(res.r_Id);
                    let GmendrukuId = document.querySelector('#GmendrukuId')
                    let GmendrukuNumber = document.querySelector('#GmendrukuNumber')
                    let GmendrukuName = document.querySelector('#GmendrukuName')
                    let GmendrukuDate = document.querySelector('#GmendrukuDate')
                    let GmendrukuWhat = document.querySelector('#GmendrukuWhat')
                    let GmendrukuOverhouse = document.querySelector('#GmendrukuOverhouse')
                    GmendrukuNumber.value = res.r_number
                    GmendrukuName.value = res.r_nameId
                    GmendrukuDate.value = res.r_date
                    GmendrukuWhat.value = res.r_what
                    GmendrukuOverhouse.value = res.r_overhouse
                    GmendrukuId.value = res.r_Id
                }
            })
        }
    })
}
//修改
function GmendrukuInfo() {
    let GmendrukuId = document.querySelector('#GmendrukuId').value
    let GmendrukuNumber = document.querySelector('#GmendrukuNumber').value
    let GmendrukuName = document.querySelector('#GmendrukuName').value
    let GmendrukuDate = document.querySelector('#GmendrukuDate').value
    let GmendrukuWhat = document.querySelector('#GmendrukuWhat').value
    let GmendrukuOverhouse = document.querySelector('#GmendrukuOverhouse').value
    if (!(GmendrukuNumber && GmendrukuName && GmendrukuDate && GmendrukuWhat && GmendrukuOverhouse && GmendrukuId)) return alert('请输入完整数据')
    Ajax({
        url: "http://localhost/h5/php/RukuGmend.php",
        type: "POST",
        data: { GmendrukuNumber, GmendrukuName, GmendrukuDate, GmendrukuWhat, GmendrukuOverhouse, GmendrukuId },
        success(res) {
            location.reload();
        }
    })

}
//封装渲染数据
function callRukuInfo(result) {
    const tbody = document.querySelector('tbody')
    tbody.innerHTML = ''
    // console.log(result);
    result.forEach(item => {
        let str = `
        <tr>
        <td>${item.r_Id}</td>
        <td>${item.r_number}</td>
        <td>${item.r_nameId}</td>
        <td>${item.r_date}</td>
        <td>${item.r_what}</td>
        <td>${item.r_overhouse}</td>
        <td>
        <button type="button" class="btn btn-warning btn-sm" data-bs-toggle="modal" data-bs-target="#rukuGmendAdd"  data-o='update' data-id='${item.r_Id}'>修改</button>
        <button type="button" class="btn btn-danger btn-sm" data-o='del' data-id='${item.r_Id}'>删除</button>
        </td>
    </tr>  
        `
        tbody.innerHTML += str
    });

}
